package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgUserIdentityMapper;
import com.ygqh.baby.po.YgUserIdentity;
import com.ygqh.baby.po.YgUserIdentityExample;
import com.ygqh.baby.service.YgUserIdentityService;
import com.ygqh.baby.utils.ASEUtils;
import com.ygqh.baby.utils.IDAuthUtil;

@Service
public class YgUserIdentityServiceImpl implements YgUserIdentityService {

	@Autowired
	private YgUserIdentityMapper ygUserIdentityMapper;

	@Override
	public List<YgUserIdentity> find() {
		YgUserIdentityExample example = new YgUserIdentityExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygUserIdentityMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgUserIdentityExample example = new YgUserIdentityExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygUserIdentityMapper.countByExample(example);
	}

	@Override
	public YgUserIdentity findById(Long id) {
		return ygUserIdentityMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgUserIdentity ygUserIdentity) {
		return ygUserIdentityMapper.insertSelective(ygUserIdentity);
	}

	@Override
	public int update(YgUserIdentity ygUserIdentity) {
		return ygUserIdentityMapper.updateByPrimaryKeySelective(ygUserIdentity);
	}

	@Override
	public ResultSet<YgUserIdentity> search(QueryInfo queryInfo, String q) {
		List<YgUserIdentity> list = ygUserIdentityMapper.selectSuper(queryInfo, q);
		int count = ygUserIdentityMapper.countSuper(q);
		ResultSet<YgUserIdentity> resultSet = new ResultSet<YgUserIdentity>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgUserIdentityExample example = new YgUserIdentityExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgUserIdentity record = new YgUserIdentity();
		record.setStatus(DataStatus.Delete);

		return ygUserIdentityMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgUserIdentity ygUserIdentity) {
		int r = 0;
		if (ygUserIdentity.getId() != null) {
			r = this.update(ygUserIdentity);
		} else {
			r = this.save(ygUserIdentity);
		}
		return r;
	}


	@Override
	public Message auth(Long userId, String idNo, String name) {
		boolean certified = this.certified(userId);
		if (certified) {
			return Message.success("已验证过。", null);
		}
		Message msg = IDAuthUtil.auth(idNo, name);

		int i = 0;
		if (Message.isSuccess(msg)) {
			YgUserIdentity ygUserIdentity = new YgUserIdentity();
			ygUserIdentity.setUserId(userId);
			ygUserIdentity.setName(name);
			ygUserIdentity.setIdNo(YgUserIdentity.encryptIdNo(idNo));
			ygUserIdentity.setCreateTime(new Date());
			i = this.save(ygUserIdentity);
		}
		if (i > 0 && Message.isSuccess(msg)) {
			return msg;
		}
		return Message.error("实名认证失败，请重试。", i);
	}
	@Override
	public boolean certified(Long userId) {
		YgUserIdentityExample example = new YgUserIdentityExample();
		example.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(DataStatus.Valid.name());
		int i = ygUserIdentityMapper.countByExample(example);

		return i > 0 ? true : false;

	}

	@Override
	public YgUserIdentity findByUserId(Long userId) {
		YgUserIdentityExample example = new YgUserIdentityExample();
		example.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(DataStatus.Valid.name());
		List<YgUserIdentity> list = ygUserIdentityMapper.selectByExample(example);

		if (CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

}
